package com.bro.broshopping;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Pair;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	// components
	private static Button btn_register;
	private static Button btn_login;
	private static EditText txt_username;
	private static EditText txt_password;
	private static TextView lbl_forgotPassword;

	/**
	 * Check in local storage whether already signed in
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // fetch components
        btn_register = (Button)findViewById(R.id.btn_register);
        btn_login = (Button)findViewById(R.id.btn_login);
        txt_username = (EditText)findViewById(R.id.txt_username);
        txt_password = (EditText)findViewById(R.id.txt_password);
        lbl_forgotPassword = (TextView)findViewById(R.id.lbl_forgotPassword);
        
        // check whether user data locally available
        Pair<String,String> userData = LocalStorage.getUserData(this);
        if (userData != null){
        	txt_username.setText(userData.first);
        	txt_password.setText(userData.second);
        }
        
        setOnClickListeners();
    }

    /**
     * Sets the onClick listeners for the views
     */
    private void setOnClickListeners() {
        btn_login.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String username = txt_username.getText().toString();
				String password = txt_password.getText().toString();
				if (!(username.equals("") && password.equals(""))){
					// check registration
				} else {
					AlertDialogManager.showAlertDialog(MainActivity.this, "Error", "Please do not leave 'Username' or 'Password' empty!");
				}
			}
		});
        
        btn_register.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// start up register view
				Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
				MainActivity.this.startActivity(intent);
			}
		});
        
        lbl_forgotPassword.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AlertDialogManager.showAlertDialog(MainActivity.this, "You're fucked!", "Well, you're really fucked...");
			}
		});
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
